package routers

import (
	"aglaia/global"
	"aglaia/internal/middleware"
	"aglaia/internal/routers/api"
	"github.com/gin-gonic/gin"
	"github.com/swaggo/gin-swagger"
	"github.com/swaggo/gin-swagger/swaggerFiles"
	"net/http"
)

func NewRouter() *gin.Engine {
	r := gin.New()
	r.Use(gin.Logger(), gin.Recovery())

	// todo: 修改登录记录为insert，而不是update （新建表 login_record ）
	r.GET("/", func(c *gin.Context) {
		c.Redirect(301, "/index.html")
	})

	r.LoadHTMLFiles("./template/index.html")

	r.GET("/index.html", func(c *gin.Context) {
		c.HTML(http.StatusOK, "index.html", nil)
	})
	//r.StaticFile("/index.html", "template/index.html")
	r.StaticFS(global.AppSetting.UploadServerUrl, http.Dir(global.AppSetting.UploadSavePath))
	r.StaticFS("/static", http.Dir("template/static"))

	r.POST("/auth", api.Auth)
	r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))

	apiv1 := r.Group("/api/v1")
	//apiv1.Use(middleware.JWT())
	{
		apiv1.GET("/articles", api.Articles)
		apiv1.GET("/article/query", api.QueryArticleById)
		apiv1.GET("/article/search", api.SearchArticles)

		apiv1.GET("/categories", api.Categories)

		apiv1.GET("/tags", api.Tags)
	}
	adminGroup := apiv1.Group("/admin")
	adminGroup.Use(middleware.JWT())
	{
		apiv1.POST("/article/create", api.CreateArticle)
		apiv1.POST("/article/update", api.UpdateArticle)
		apiv1.POST("/articles/delete", api.DeleteArticle)

		apiv1.POST("/category/create", api.CreateCategory)
		apiv1.POST("/category/delete", api.DeleteCategory)

		apiv1.POST("/tag/first", api.FirstOrCreateTag) // first or create
		apiv1.POST("/tag/delete", api.DeleteTag)
	}

	return r
}
